package com.hexinfo.dmpro.dev.data.transfer.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hexinfo.dmpro.common.base.PageResult;
import com.hexinfo.dmpro.common.utils.CommonConstants;
import com.hexinfo.dmpro.dev.data.transfer.dao.DesensitizeFieldFilterMapper;
import com.hexinfo.dmpro.dev.data.transfer.model.DesensitizeFieldFilter;
import com.hexinfo.dmpro.dev.data.transfer.service.IDesensitizeFieldFilterService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Package com.hexinfo.dmpro.dev.data.transfer.service.impl
 * @ClassName DesensitizeFieldFilterServiceImpl
 * @Description 脱敏字段黑名单业务实现
 * @Author Wang zhihao
 * @Date 2024/1/8 10:07
 * @Version v1.0
 **/
@Service
public class DesensitizeFieldFilterServiceImpl extends ServiceImpl<DesensitizeFieldFilterMapper, DesensitizeFieldFilter> implements IDesensitizeFieldFilterService {

    /**
     * @Method queryList
     * @Param
     * @param page
     * @param desensitizeFieldFilter
     * @Return com.hexinfo.dmpro.common.base.PageResult
     * @Description 分页查询
     * @Author Wang zhihao
     * @Date 2024/1/8 10:08
     * @Version V1.0
     */
    @Override
    public PageResult queryList(Page page, DesensitizeFieldFilter desensitizeFieldFilter) {
        LambdaQueryWrapper<DesensitizeFieldFilter> wrapper = beforeQuery(desensitizeFieldFilter);
        Page returnPage = this.baseMapper.selectPage(page, wrapper);
        return new PageResult(returnPage.getRecords(), returnPage.getTotal());
    }

    /**
     * @Method beforeQuery
     * @Param
     * @param desensitizeFieldFilter
     * @Return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.hexinfo.dmpro.dev.data.transfer.model.DesensitizeFieldFilter>
     * @Description 搜索条件设置
     * @Author Wang zhihao
     * @Date 2024/1/8 13:55
     * @Version V1.0
     */
    private LambdaQueryWrapper<DesensitizeFieldFilter> beforeQuery(DesensitizeFieldFilter desensitizeFieldFilter) {
        LambdaQueryWrapper<DesensitizeFieldFilter> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DesensitizeFieldFilter::getDel, CommonConstants.STATUS_NORMAL);
        if (!StrUtil.isEmpty(desensitizeFieldFilter.getDatabaseName())) {
            wrapper.like(DesensitizeFieldFilter::getDatabaseName, desensitizeFieldFilter.getDatabaseName());
        }
        if (!StrUtil.isEmpty(desensitizeFieldFilter.getTableName())) {
            wrapper.like(DesensitizeFieldFilter::getTableName, desensitizeFieldFilter.getTableName());
        }
        if (!StrUtil.isEmpty(desensitizeFieldFilter.getFieldName())) {
            wrapper.like(DesensitizeFieldFilter::getFieldName, desensitizeFieldFilter.getFieldName());
        }
        return wrapper;
    }

    /**
     * @Method queryListNoPage
     * @Param
     * @param desensitizeFieldFilter
     * @Return java.util.List<com.hexinfo.dmpro.dev.data.transfer.model.DesensitizeFieldFilter>
     * @Description 不分页查询
     * @Author Wang zhihao
     * @Date 2024/1/8 10:09
     * @Version V1.0
     */
    @Override
    public List<DesensitizeFieldFilter> queryListNoPage(DesensitizeFieldFilter desensitizeFieldFilter) {
        LambdaQueryWrapper<DesensitizeFieldFilter> wrapper = beforeQuery(desensitizeFieldFilter);
        return this.baseMapper.selectList(wrapper);
    }

    /**
     * @Method save
     * @Param
     * @param desensitizeFieldFilter
     * @Return boolean
     * @Description 新增黑名单
     * @Author Wang zhihao
     * @Date 2024/1/8 13:24
     * @Version V1.0
     */
    @Override
    public boolean save(DesensitizeFieldFilter desensitizeFieldFilter) {
        // 校验是否有字段已存在
        isExist(desensitizeFieldFilter);
        List<String> fieldList = desensitizeFieldFilter.getFieldNameList();
        for (String field : fieldList) {
            desensitizeFieldFilter.setFieldName(field);
            this.baseMapper.insert(desensitizeFieldFilter);
        }
        return true;
    }

    /**
     * @Method isExist
     * @Param
     * @param desensitizeFieldFilter
     * @Return boolean
     * @Description 检验添加的字段是否已存在
     * @Author Wang zhihao
     * @Date 2024/1/8 13:35
     * @Version V1.0
     */
    boolean isExist(DesensitizeFieldFilter desensitizeFieldFilter) {
        if (StrUtil.isNotEmpty(desensitizeFieldFilter.getFieldName())){
            List<String> fieldNameList = new ArrayList<>();
            fieldNameList.add(desensitizeFieldFilter.getFieldName());
            desensitizeFieldFilter.setFieldNameList(fieldNameList);
        }
        for (String field : desensitizeFieldFilter.getFieldNameList()) {
            LambdaQueryWrapper<DesensitizeFieldFilter> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(DesensitizeFieldFilter::getDatabaseName, desensitizeFieldFilter.getDatabaseName());
            wrapper.eq(DesensitizeFieldFilter::getTableName, desensitizeFieldFilter.getTableName());
            wrapper.eq(DesensitizeFieldFilter::getFieldName, field);
            if (count(wrapper) > 0) {
                throw new RuntimeException(StrUtil.format("所添加的字段【{}.{}】-【{}】已存在，请检查",
                        desensitizeFieldFilter.getDatabaseName(), desensitizeFieldFilter.getTableName(), field));
            }
        }
        return true;
    }

    /**
     * @Method removeByIds
     * @Param
     * @param ids
     * @Return boolean
     * @Description 批量删除
     * @Author Wang zhihao
     * @Date 2024/1/8 10:09
     * @Version V1.0
     */
    @Override
    public boolean removeByIds(List<String> ids) {
        ids.forEach(this::removeById);
        return true;
    }
}
